package com.youlai.boot.system.controller;
import com.youlai.boot.common.enums.LogModuleEnum;
import com.youlai.boot.common.annotation.RepeatSubmit;
import com.youlai.boot.common.model.Option;
import com.youlai.boot.common.result.Result;
import com.youlai.boot.system.model.form.ArticleTypeForm;
import com.youlai.boot.system.model.query.ArticleTypeQuery;
import com.youlai.boot.system.model.vo.ArticleTypeVO;
import com.youlai.boot.common.annotation.Log;
import com.youlai.boot.system.service.ArticleTypeService;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;
import java.util.List;

/**
 * 文章分类控制器
 * @author haoxr
 * @since 2020/11/6
 */
@Tag(name = "05.文章分类接口")
@RestController
@RequestMapping("/api/v1/articletype")
@RequiredArgsConstructor
public class ArticleTypeController {

    private final ArticleTypeService ArticleTypeService;

    @Operation(summary = "文章分类列表")
    @GetMapping
    @Log( value = "文章分类列表",module = LogModuleEnum.DEPT)
    public Result<List<ArticleTypeVO>> getArticleTypeList(
            ArticleTypeQuery queryParams
    ) {
        List<ArticleTypeVO> list = ArticleTypeService.getArticleTypeList(queryParams);
        return Result.success(list);
    }

    @Operation(summary = "文章分类下拉列表")
    @GetMapping("/options")
    public Result<List<Option<Long>>> getArticleTypeOptions() {
        List<Option<Long>> list = ArticleTypeService.listArticleTypeOptions();
        return Result.success(list);
    }

    @Operation(summary = "新增文章分类")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('sys:articletype:add')")
    @RepeatSubmit
    public Result<?> saveDept(
            @Valid @RequestBody ArticleTypeForm formData
    ) {
        Long id = ArticleTypeService.saveArticleType(formData);
        return Result.success(id);
    }

    @Operation(summary = "获取文章分类表单数据")
    @GetMapping("/{articletypeId}/form")
    public Result<ArticleTypeForm> getArticleTypeForm(
            @Parameter(description ="文章分类ID") @PathVariable Long articletypeId
    ) {
        ArticleTypeForm ArticleTypeForm = ArticleTypeService.getArticleTypeForm(articletypeId);
        return Result.success(ArticleTypeForm);
    }

    @Operation(summary = "修改文章分类")
    @PutMapping(value = "/{articletypeId}")
    @PreAuthorize("@ss.hasPerm('sys:articletype:edit')")
    public Result<?> updateArticleType(
            @PathVariable Long articletypeId,
            @Valid @RequestBody ArticleTypeForm formData
    ) {
        articletypeId = ArticleTypeService.updateArticleType(articletypeId, formData);
        return Result.success(articletypeId);
    }

    @Operation(summary = "删除文章分类")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('sys:articletype:delete')")
    public Result<?> deleteDepartments(
            @Parameter(description ="文章分类ID，多个以英文逗号(,)分割") @PathVariable("ids") String ids
    ) {
        boolean result = ArticleTypeService.deleteByIds(ids);
        return Result.judge(result);
    }

}